﻿
using UnityEngine;

namespace XFGameFramework.TweenSystem
{
    /// <summary>
    /// RectTransform动画拓展
    /// </summary>
    public static class RectTransformExtensions 
    {
        /// <summary>
        /// 渐变AnchoredPosition
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标位置</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenAnchoredPosition(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(rectTransform, rectTransform.anchoredPosition, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenAnchoredPosition, delay, 0, loopCount);
        }

        internal static void OnTweenAnchoredPosition(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.anchoredPosition = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变AnchoredPosition3D
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标位置</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector3> TweenAnchoredPosition3D(this RectTransform rectTransform, Vector3 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(rectTransform, rectTransform.anchoredPosition3D, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenAnchoredPosition3D, delay, 0, loopCount);
        }

        internal static void OnTweenAnchoredPosition3D(TweenerCore<Vector3> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.anchoredPosition3D = tweener.Value;
            }
        }


        /// <summary>
        /// 渐变AnchorMin
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标AnchorMin</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenAnchorMin(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {  
            return TweenManager.ComponentTween(rectTransform, rectTransform.anchorMin, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenAnchorMin, delay,0,loopCount);
        }


        internal static void OnTweenAnchorMin(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.anchorMin = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变AnchorMax
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标AnchorMax</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenAnchorMax(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(rectTransform, rectTransform.anchorMax, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenAnchorMax, delay,0,loopCount);
        }

        internal static void OnTweenAnchorMax(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.anchorMax = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变OffsetMin
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标OffsetMin</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenOffsetMin(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(rectTransform, rectTransform.offsetMin, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenOffsetMin, delay,0,loopCount);
        }

        internal static void OnTweenOffsetMin(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.offsetMin = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变OffsetMax
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标OffsetMax</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenOffsetMax(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(rectTransform, rectTransform.offsetMax, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenOffsetMax, delay,0,loopCount);
        }


        internal static void OnTweenOffsetMax(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.offsetMax = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变Pivot
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标Pivot</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenPivot(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {  
            return TweenManager.ComponentTween(rectTransform, rectTransform.pivot, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenPivot, delay,0,loopCount);
        }

        internal static void OnTweenPivot(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.pivot = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变SizeDelta
        /// </summary>
        /// <param name="rectTransform">rectTransform对象</param>
        /// <param name="target">目标SizeDelta</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenSizeDelta(this RectTransform rectTransform, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(rectTransform, rectTransform.sizeDelta, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenSizeDelta, delay,0,loopCount);
        }

        internal static void OnTweenSizeDelta(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is RectTransform)
            {
                RectTransform rectTransform = tweener.Component as RectTransform;
                rectTransform.sizeDelta = tweener.Value;
            }
        }

    }

}

